﻿using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Adams.Web.Data.Home
{
    public static class Index
    {
        public static Adams.Web.Models.Home.Index.PageLoad PageLoad()
        {
            var data = new Adams.Web.Models.Home.Index.PageLoad();

            using (SqlDataReader dr = DB.RunDataReader(@"
SET NOCOUNT ON

SELECT TOP 5 O.*, E.CalendarID, C.Color, C.Name AS CalendarName
FROM Events.Occurances O
INNER JOIN Events.Events E ON O.EventID = E.EventID
INNER JOIN Events.Calendars C ON C.CalendarID = E.CalendarID
WHERE DATEDIFF(d, GETDATE(), O.StartDate) >= 0
ORDER BY O.StartDate

SELECT A.*
FROM Announcements.Announcements A
WHERE DATEDIFF(d, GetDate(), A.EndDate) > 0 OR DATEDIFF(d, GetDate(), A.StartDate) > 0

SELECT TOP 3 P.*
FROM PressReleases.PressReleases P
ORDER BY P.ReleaseDate DESC

SELECT TOP 3 P.*
FROM News.Articles P
ORDER BY P.ReleaseDate DESC

DECLARE @LocationID int
SELECT @LocationID = LocationID
FROM DailyPrayers.Locations 
WHERE IsMaster = 1

SELECT * 
FROM DailyPrayers.PrayerTimes 
WHERE LocationID = @LocationID AND Month = DATEPART(m, GETDATE()) AND Day = DATEPART(d, GETDATE())

SELECT * FROM 
JummahPrayers.Locations 
ORDER BY DisplayOrder

SELECT * 
FROM JummahPrayers.Prayers
ORDER BY DisplayOrder

SELECT * FROM 
TarawehPrayers.Locations 
ORDER BY DisplayOrder

SELECT * 
FROM TarawehPrayers.Prayers
ORDER BY DisplayOrder

SELECT * FROM 
EidPrayers.Locations 
ORDER BY DisplayOrder

SELECT * 
FROM EidPrayers.Prayers
ORDER BY DisplayOrder
", CommandType.Text))
            {
                data.Events = new Adams.Shared.Entities.Events.OccuranceCollection();
                while (dr.Read())
                {
                    var evet = DB.InitFromReader<Shared.Entities.Events.Occurance>(dr, new ColMap { NameInDb = "OccuranceID", NameInObject = "ID" });
                    evet.Event = new Shared.Entities.Events.Event()
                    {
                        ID = dr["EventID"].DbInt(),
                        Calendar = new Shared.Entities.Events.Calendar() {
                            ID = dr["CalendarID"].DbInt(),
                            Name = dr["CalendarName"].DbString(),
                            Color = dr["Color"].DbString()
                        }
                    };
                    data.Events.Add(evet);
                }

                dr.NextResult();
                data.Announcements = new Adams.Shared.Entities.Announcements.AnnouncementCollection();
                while (dr.Read())
                {
                    var announcement = DB.InitFromReader<Shared.Entities.Announcements.Announcement>(dr, new ColMap { NameInDb = "AnnouncementID", NameInObject = "ID" });
                    data.Announcements.Add(announcement);
                }

                dr.NextResult();
                data.PressReleases = new Adams.Shared.Entities.PressReleases.PressReleaseCollection();
                while (dr.Read())
                {
                    var pressrelease = DB.InitFromReader<Shared.Entities.PressReleases.PressRelease>(dr, new ColMap { NameInDb = "PressReleaseID", NameInObject = "ID" });
                    data.PressReleases.Add(pressrelease);
                }

                dr.NextResult();
                data.News = new Adams.Shared.Entities.News.ArticleCollection();
                while (dr.Read())
                {
                    var article = DB.InitFromReader<Shared.Entities.News.Article>(dr, new ColMap { NameInDb = "ArticleID", NameInObject = "ID" });
                    data.News.Add(article);
                }

                dr.NextResult();
                dr.Read();
                data.PrayerTime = DB.InitFromReader<Shared.Entities.DailyPrayers.PrayerTime>(dr, new ColMap { NameInDb = "PrayerID", NameInObject = "ID" });

                dr.NextResult();
                data.JummahLocations = new Adams.Shared.Entities.JummahPrayers.LocationCollection();
                while (dr.Read())
                {
                    var jummahlocation = DB.InitFromReader<Shared.Entities.JummahPrayers.Location>(dr, new ColMap { NameInDb = "LocationID", NameInObject = "ID" });
                    jummahlocation.Prayers = new Adams.Shared.Entities.JummahPrayers.PrayerCollection();
                    data.JummahLocations.Add(jummahlocation);
                }

                dr.NextResult();
                while (dr.Read())
                {
                    int locationid = dr["LocationID"].DbInt();
                    var jprayer = DB.InitFromReader<Shared.Entities.JummahPrayers.Prayer>(dr, new ColMap { NameInDb = "PrayerID", NameInObject = "ID" });
                    data.JummahLocations.Where(l => l.ID == locationid).FirstOrDefault().Prayers.Add(jprayer);
                }

                dr.NextResult();
                data.TarawehLocations = new Shared.Entities.TarawehPrayers.LocationCollection();
                while (dr.Read())
                {
                    var tlocation = DB.InitFromReader<Shared.Entities.TarawehPrayers.Location>(dr, new ColMap { NameInDb = "LocationID", NameInObject = "ID" });
                    tlocation.Prayers = new Adams.Shared.Entities.TarawehPrayers.PrayerCollection();
                    data.TarawehLocations.Add(tlocation);
                }

                dr.NextResult();
                while (dr.Read())
                {
                    int locationid = dr["LocationID"].DbInt();
                    var jprayer = DB.InitFromReader<Shared.Entities.TarawehPrayers.Prayer>(dr, new ColMap { NameInDb = "PrayerID", NameInObject = "ID" });
                    data.TarawehLocations.Where(l => l.ID == locationid).FirstOrDefault().Prayers.Add(jprayer);
                }

                dr.NextResult();
                data.EidLocations = new Shared.Entities.EidPrayers.LocationCollection();
                while (dr.Read())
                {
                    var elocation = DB.InitFromReader<Shared.Entities.EidPrayers.Location>(dr, new ColMap { NameInDb = "LocationID", NameInObject = "ID" });
                    elocation.Prayers = new Adams.Shared.Entities.EidPrayers.PrayerCollection();
                    data.EidLocations.Add(elocation);
                }

                dr.NextResult();
                while (dr.Read())
                {
                    int locationid = dr["LocationID"].DbInt();
                    var jprayer = DB.InitFromReader<Shared.Entities.EidPrayers.Prayer>(dr, new ColMap { NameInDb = "PrayerID", NameInObject = "ID" });
                    data.EidLocations.Where(l => l.ID == locationid).FirstOrDefault().Prayers.Add(jprayer);
                }

                return data;
            }
        }
    }
}